package com.xinyu.service.impl;

import com.xinyu.config.FileStorageProperties;
import com.xinyu.domain.Public;
import com.xinyu.mapper.PublicMapper;
import com.xinyu.mapper.SiteMapper;
import com.xinyu.mapper.UserMapper;
import com.xinyu.service.IPublicService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinyu.vo.BaseResult;
import com.xinyu.vo.PublicRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author: XinYu
 * @since 2025-02-16
 */
@Service
public class PublicServiceImpl extends ServiceImpl<PublicMapper, Public> implements IPublicService {

    @Autowired
    private PublicMapper publicMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private SiteMapper siteMapper;

    @Autowired
    private FileStorageProperties fileStorageProperties; // 注入配置类

    @Override
    public BaseResult queryByCondition(PublicRequest publicRequest) {
        List<Public> publicList = publicMapper.selectPublics(publicRequest);
        Integer publicCounts = publicMapper.publicCounts(publicRequest);

        for (Public pub:publicList){
            if (StringUtils.isNotBlank(pub.getUser().getHead())){
                if (!pub.getUser().getHead().contains(fileStorageProperties.getBaseUrl()))
                    pub.getUser().setHead(fileStorageProperties.getBaseUrl() + pub.getUser().getHead());
            }
            if (StringUtils.isNotBlank(pub.getAttachment())) {
                if (!pub.getAttachment().contains(fileStorageProperties.getBaseUrl())) {
                    pub.setAttachment(fileStorageProperties.getBaseUrl() + pub.getAttachment());
                }
            }
        }
        return BaseResult.ok(publicList,publicCounts);
    }

    @Override
    public Public queryPublicById(Integer publicId) {
        Public pub = publicMapper.selectById(publicId);
        pub.setUser(userMapper.selectById(pub.getUserId()));
        pub.setSite(siteMapper.selectById(pub.getSiteId()));
        if (StringUtils.isNotBlank(pub.getUser().getHead())) {
            if (!pub.getUser().getHead().contains(fileStorageProperties.getBaseUrl())) {
                pub.getUser().setHead(fileStorageProperties.getBaseUrl() + pub.getUser().getHead());
            }
        }
        if (StringUtils.isNotBlank(pub.getAttachment())) {
            if (!pub.getAttachment().contains(fileStorageProperties.getBaseUrl())) {
                pub.setAttachment(fileStorageProperties.getBaseUrl() + pub.getAttachment());
            }
        }
        return pub;
    }


}
